<template>
	<view class="pb80 bf">
		<view class="tabs dfa f16">
			<view class="tab tac f-c-c" :class="current == i?'isTab':''" v-for="(v,i) in tabs" :key="i"
				:style="{color:current === i?'#000':'#666'}" @click="handTab(v,i)">{{v.name}}
				<view class="line" :style="{background: current === i?subColor:'#fff'}"></view>
			</view>
		</view>
		<view class="f-bt f-y-c bf p2">
			<view class="f-g-1">
				<u-input placeholder="请输入商品名称" v-model="queryForm.name" @change="fetchData" prefixIcon="search"
					shape="circle" prefixIconStyle="font-size: 22px;color: #666;margin-right:10rpx;padding-left:10px"
					placeholderStyle="color:#666" customStyle="background:#f8f8f8;padding:15rpx 5rpx;" border="none"
					clearable v-if="currVal=='bzsp' || currVal=='tcsp'">
				</u-input>
				<u-input v-else placeholder="请输入商品名称" v-model="queryForm.keyword" @change="fetchData"
					prefixIcon="search" shape="circle"
					prefixIconStyle="font-size: 22px;color: #666;margin-right:10rpx;padding-left:10px"
					placeholderStyle="color:#666" customStyle="background:#f8f8f8;padding:15rpx 5rpx;" border="none"
					clearable>
				</u-input>
			</view>
		</view>
		<view class="p10 c6 bff1">
			<view class="f-1" style="overflow: auto;">
				<view class="types" style="overflow-x: auto;">
					<view class="type c0 f12" :class="typeNum === index?'isType':''" v-for="(item,index) in tab2"
						:key="index"
						:style="{background:typeNum === index?'linear-gradient(to right, #fee14f, #fdc450)':'',fontWeight:typeNum===index?'bold':''}"
						@click="typeChange(index,item)">{{item.name}}
					</view>
				</view>
			</view>
		</view>
		<view class="uni-container mt10">
			<uni-table ref="table" border stripe emptyText="暂无明细" v-if="currVal=='bzsp' || currVal=='tcsp'">
				<uni-tr>
					<uni-th align="center">排序</uni-th>
					<uni-th align="center">商品ID</uni-th>
					<uni-th align="center">商品名称</uni-th>
					<uni-th align="center">商品渠道</uni-th>
					<uni-th align="center" v-if="queryForm.state!='recycle'">商品库价格</uni-th>
					<uni-th align="center">实际销量</uni-th>
					<uni-th align="center">创建时间</uni-th>
					<uni-th align="center">操作</uni-th>
				</uni-tr>
				<uni-tr v-for="(row, i) in list" :key="i">
					<uni-td align="center">{{ row.sort }}</uni-td>
					<uni-td align="center">{{ row.id }}</uni-td>
					<uni-td align="center">
						<view class="flex name">
							<view class="f-c f-g-0 avatar">
								<image class="wh bs10" :src="row.logo" mode="" />
							</view>
							<view class="f-g-1 rBox ml5">
								<view class="flex">
									<view>{{ row.name }}</view>
									<view class="r_logo mr5" v-if="row.specSwitch == 1">多</view>
									<view class="r_logo_g mr5" v-if="row.isShow == 1">展</view>
								</view>
								<view class="f12 c6 tags flex">
									<text v-for="(v, i) in row.category" :key="i">
										<text>{{ v.name }}</text>
									</text>
								</view>
							</view>
						</view>
					</uni-td>
					<uni-td align="center">
						<view class="f-c">
							<u-tag v-if="row.channelIds && row.channelIds.includes(1)" text="外卖" plain shape="circle"
								class="mr5"></u-tag>
							<u-tag v-if="row.channelIds && row.channelIds.includes(2)" text="店内" plain shape="circle"
								type="success"></u-tag>
						</view>
					</uni-td>
					<uni-td align="center" v-if="queryForm.state!='recycle'">
						<view>
							<view v-if="row.specSwitch == 1">{{ row.skus_min_price }}~{{ row.skus_max_price }}</view>
							<view v-else>{{ row.singleSpec && row.singleSpec.price }}</view>
						</view>
					</uni-td>
					<uni-td align="center">{{ row.sales }}</uni-td>
					<uni-td align="center">{{ row.created_at }}</uni-td>
					<uni-td align="center">
						<text class="cf06 mr10" @click.stop="handEditGoods(row)">编辑</text>
						<text class="cf06 mr10" @click.stop="handCopyGoods(row)">复制</text>
						<text class="cf06 mr10" @click.stop="handsSXj(row)" v-if="queryForm.state=='shelf'">下架</text>
						<text class="cf06 mr10" @click.stop="handsSXj(row)" v-if="queryForm.state=='offShelf'">上架</text>
						<text class="cf06 mr10" @click.stop="handDel(row)" v-if="queryForm.state=='offShelf'">删除</text>
						<text class="cf06 mr10" @click.stop="handRestore(row)" v-if="queryForm.state=='recycle'">恢复</text>
						<text class="cf06 mr10" @click.stop="handDels(row)" v-if="queryForm.state=='recycle'">彻底删除</text>
					</uni-td>
				</uni-tr>
			</uni-table>
			<uni-table ref="table" border stripe emptyText="暂无明细" v-if="currVal=='jlsp' && queryForm.state=='t-jlsp'">
				<uni-tr>
					<uni-th align="center">排序</uni-th>
					<uni-th align="center">加料名称</uni-th>
					<uni-th align="center">所属分组</uni-th>
					<uni-th align="center">售价</uni-th>
					<uni-th align="center">初始库存</uni-th>
					<uni-th align="center">创建时间</uni-th>
					<uni-th align="center">是否启用</uni-th>
					<uni-th align="center">操作</uni-th>
				</uni-tr>
				<uni-tr v-for="(row, i) in list" :key="i">
					<uni-td align="center">{{ row.sort }}</uni-td>
					<uni-td align="center">
						<view class="name">{{ row.name }}</view>
					</uni-td>
					<uni-td align="center">
						<view>{{ row.category && row.category.name }}</view>
					</uni-td>
					<uni-td align="center">
						<view>{{ row.price }}</view>
					</uni-td>
					<uni-td align="center">
						<view>{{ row.inventory }}</view>
					</uni-td>
					<uni-td align="center">
						<view>{{ row.created_at }}</view>
					</uni-td>
					<uni-td align="center">
						<view>
							<u-switch v-model="row.state" :inactiveValue="2" :activeValue="1" :activeColor="subColor"
								size="20">
							</u-switch>
						</view>
					</uni-td>
					<uni-td align="center">
						<text class="cf06 mr10" @click.stop="handEdit(row)">编辑</text>
						<text class="cf06 mr10" @click.stop="handDel(row)">删除</text>
					</uni-td>
				</uni-tr>
			</uni-table>
			<uni-table ref="table" border stripe emptyText="暂无明细" v-if="currVal=='jlsp' && queryForm.state=='t-jlfz'">
				<uni-tr>
					<uni-th align="center">排序</uni-th>
					<uni-th align="center">分组名称</uni-th>
					<uni-th align="center">创建时间</uni-th>
					<uni-th align="center">操作</uni-th>
				</uni-tr>
				<uni-tr v-for="(row, i) in list" :key="i">
					<uni-td align="center">{{ row.sort }}</uni-td>
					<uni-td align="center">
						<view class="name">{{ row.name }}</view>
					</uni-td>
					<uni-td align="center">
						<view>{{ row.created_at }}</view>
					</uni-td>
					<uni-td align="center">
						<text class="cf06 mr10" @click.stop="handEdit(row)">编辑</text>
						<text class="cf06 mr10" @click.stop="handDel(row)">删除</text>
					</uni-td>
				</uni-tr>
			</uni-table>
			<view class="uni-pagination-box"><uni-pagination show-icon :page-size="queryForm.pageSize"
					:current="queryForm.pageNo" :total="total" @change="change" /></view>
		</view>
		<view class="buts bf5">
			<view class="p-0-20 mb10">
				<view class="cf bs30 tac p10 f14 wei" :style="{background:subColor,color:'#000'}" @click="handAdd">
					<text v-if="currVal=='bzsp'">发布商品</text>
					<text v-if="currVal=='tcsp'">发布套餐商品</text>
					<text v-if="currVal=='jlsp' && queryForm.state=='t-jlsp'">创建加料商品</text>
					<text v-if="currVal=='jlsp' && queryForm.state=='t-jlfz'">创建加料分组</text>
				</view>
			</view>
		</view>
		<addJl ref="addJl" @save="fetchData"></addJl>
		<addJlFz ref="addJlFz" @save="fetchData"></addJlFz>
	</view>
</template>

<script>
	import addJl from "./components/addJl.vue"
	import addJlFz from "./components/addJlFz.vue"
	export default {
		name: 'flsz',
		components: {
			addJl,
			addJlFz,
		},
		data() {
			return {
				subColor: uni.getStorageSync('subject_color'),
				current: 0,
				currVal: 'bzsp',
				tabs: [{
					name: '标准商品',
					value: 'bzsp',
				}, {
					name: '套餐商品',
					value: 'tcsp',
				}, {
					name: '加料商品',
					value: 'jlsp',
				}],
				queryForm: {
					name: "",
					pageNo: 1,
					pageSize: 10,
					type: 1,
					state: 'shelf',
				},
				apiPage: 'goodsList',
				list: [],
				typeNum: 0,
				tab2: [{
					name: '已上架',
					value: 'shelf',
				}, {
					name: '已下架',
					value: 'offShelf',
				}, {
					name: '回收站',
					value: 'recycle',
				}],
				total:0,
			}
		},
		onLoad() {
			this.fetchData()
		},
		computed: {},
		methods: {
			async handTab(v, i) {
				this.current = i
				this.currVal = v.value
				this.loading = true
				this.queryForm.state = ''
				this.queryForm.pageNo = 1
				this.typeNum = 0
				if (v.value == 'jlsp') {
					this.queryForm.state = 't-jlsp'
				}else{
					this.queryForm.state = 'shelf'
				}
				await this.fetchData()
				this.loading = false
			},
			typeChange(i, v) {
				this.typeNum = i
				this.queryForm.state = v.value
				this.queryForm.pageNo = 1
				this.fetchData()
			},
			async fetchData(type) {
				if (this.currVal == 'bzsp' || this.currVal == 'tcsp') {
					this.tab2 = [{
						name: '已上架',
						value: 'shelf',
					}, {
						name: '已下架',
						value: 'offShelf',
					}, {
						name: '回收站',
						value: 'recycle',
					}]
				} else {
					this.tab2 = [{
						name: '加料商品',
						value: 't-jlsp',
					}, {
						name: '加料分组',
						value: 't-jlfz',
					}]
				}
				if (this.currVal == 'bzsp') {
					this.apiPage = 'goodsList'
					this.queryForm.type = 1
				} else if (this.currVal == 'tcsp') {
					this.apiPage = 'goodsList'
					this.queryForm.type = 2
				} else {
					if (this.queryForm.state == 't-jlsp') {
						this.apiPage = 'materialList'
					} else if (this.queryForm.state == 't-jlfz') {
						this.apiPage = 'materialCatList'
					}
				}
				this.status = 'loading'
				let {
					data: {
						list,
						pageNo,
						pageSize,
						total
					},
				} = await this.beg.request({
					url: `${this.api[this.apiPage]}`,
					data: this.queryForm,
				})
				this.total = total
				this.queryForm.pageNo = pageNo
				this.queryForm.pageSize = pageSize
				this.list = list ? list : []
				// if (type != 'nextPage' || this.queryForm.pageNo == 1) {
				// 	this.list = list
				// } else {
				// 	if (list.length == 0) {
				// 		this.status = 'nomore'
				// 		return
				// 	}
				// 	this.list = this.list.concat(list)
				// }
				// this.status = 'loadmore'
			},
			change(e) {
				this.queryForm.pageNo = e.current;
				this.fetchData()
			},
			handDel(v) {
				uni.showModal({
					title: '温馨提示',
					content: '你确定要删除当前项吗',
					success: async (res) => {
						if (res.confirm) {
							let {
								msg
							} = await this.beg.request({
								url: `${this.api[this.apiPage]}/${v.id}`,
								method: "DELETE",
							})
							this.fetchData()
							uni.$u.toast(msg)
						}
					}
				});
			},
			handsSXj(v){
				uni.showModal({
					title: '温馨提示',
					content: '你确定要修改当前状态吗',
					success: async (res) => {
						if (res.confirm) {
							let {
								msg
							} = await this.beg.request({
								url: `${this.api.goodsState}/${v.id}`,
								method: "POST",
							})
							this.fetchData()
							uni.$u.toast(msg)
						}
					}
				});
			},
			handRestore(v){
				uni.showModal({
					title: '温馨提示',
					content: '你确定要恢复当前商品吗',
					success: async (res) => {
						if (res.confirm) {
							let {
								msg
							} = await this.beg.request({
								url: `${this.api.restore}/${v.id}`,
								method: "POST",
							})
							this.fetchData()
							uni.$u.toast(msg)
						}
					}
				});
			},
			handDels(v){
				uni.showModal({
					title: '温馨提示',
					content: '你确定要彻底删除当前商品吗',
					success: async (res) => {
						if (res.confirm) {
							let {
								msg
							} = await this.beg.request({
								url: `${this.api.forceDelete}/${v.id}`,
								method: "DELETE",
							})
							this.fetchData()
							uni.$u.toast(msg)
						}
					}
				});
			},
			handEditGoods(v){
				if (this.currVal == 'bzsp') {
					uni.navigateTo({
						url: `/pages/mine/spgl/fbsp?id=${v.id}&type=1`
					})
				}else if(this.currVal == 'tcsp'){
					uni.navigateTo({
						url: `/pages/mine/spgl/fbtcsp?id=${v.id}&type=2`
					})
				}
			},
			handCopyGoods(v){
				if (this.currVal == 'bzsp') {
					uni.navigateTo({
						url: `/pages/mine/spgl/fbsp?id=${v.id}&type=copy`
					})
				}else if(this.currVal == 'tcsp'){
					uni.navigateTo({
						url: `/pages/mine/spgl/fbtcsp?id=${v.id}&type=copy`
					})
				}
			},
			handEdit(row) {
				if (this.currVal == 'bzsp') {
					this.$refs['addArea'].open()
				} else if(this.currVal == 'tcsp') {
					this.$refs['addLabel'].open()
				}else if(this.currVal == 'jlsp') {
					if(this.queryForm.state == 't-jlsp'){
						this.$refs['addJl'].open(row)
					}else[
						this.$refs['addJlFz'].open(row)
					]
				}
			},
			handAdd() {
				if (this.currVal == 'bzsp') {
					uni.navigateTo({
						url: `/pages/mine/spgl/fbsp?type=1`
					})
				} else if(this.currVal == 'tcsp') {
					uni.navigateTo({
						url: `/pages/mine/spgl/fbtcsp?type=2`
					})
				}else if(this.currVal == 'jlsp') {
					if(this.queryForm.state == 't-jlsp'){
						this.$refs['addJl'].open()
					}else[
						this.$refs['addJlFz'].open()
					]
				}
			},
		},
	}
</script>
<style scoped lang="scss">
	.tabs {
		padding: 5rpx 0 0 0;
		border-radius: 15rpx;

		.tab {
			padding: 15rpx;
			width: 50%;
			color: #666;
		}

		.isTab {
			border-radius: 15rpx;
			font-weight: 600;
		}

		.line {
			margin-top: 10rpx;
			width: 30rpx;
			height: 8rpx;
			margin-left: 12rpx;
		}
	}

	.buts {
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 0;
	}

	.pplg {
		width: 40rpx;
		height: 40rpx;
		border-radius: 10rpx;
	}

	.bff1 {
		background: #f1f1f1;
	}

	.types {
		width: 650px;
		height: 54rpx;

		.type {
			display: inline-block;
			margin-right: 20rpx;
			padding: 10rpx 20rpx;
			height: 50rpx;
			border-radius: 8rpx;
			background: #fff;
		}
	}

	.name {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;

		.rBox {
			height: 50px;
			flex: 1;
			margin-left: 5px;
			display: flex;
			flex-direction: column;
			align-items: flex-start;
			justify-content: space-between;

			.r_logo {
				display: inline-block;
				margin-right: 5px;
				width: 18px;
				height: 18px;
				color: #136ffe;
				background-color: #f0faff;
				text-align: center;
				line-height: 18px;
				font-size: 12px;
			}

			.r_logo_g {
				display: inline-block;
				margin-right: 5px;
				width: 18px;
				height: 18px;
				color: #67c23a;
				background-color: #f0f9eb;
				text-align: center;
				line-height: 18px;
				font-size: 12px;
			}

			.tags {
				width: 100%;
				height: 25px;
				max-width: 280px;
				padding-right: 18px;
				box-sizing: border-box;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;

				&>text {
					display: inline-block;
					margin-right: 10px;
					padding: 1px 8px;
					border-radius: 2px;
					color: #666;
					background: #f4f6f8;
					text-align: center;
					line-height: 17px;
				}
			}
		}
	}

	.avatar {
		width: 80rpx;
		height: 80rpx;
	}
</style>